<template>
    <div class="login-wrap">
        <div class="ms-title">后台管理系统</div>
        <div class="ms-login">
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="0px" class="demo-loginForm">
                <el-form-item prop="user_name">
                    <el-input v-model="loginForm.user_name" placeholder="请输入用户名："></el-input>
                </el-form-item>
                <el-form-item prop="user_password">
                    <el-input type="password" placeholder="请输入密码：" v-model="loginForm.user_password" @keyup.enter.native="submitForm('loginForm')"></el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
                <p style="font-size:12px;line-height:30px;color:#999;">还没有账号 <a href="./#/register">立即注册</a></p>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data: function() {
    return {
      loginForm: {
        user_name: "",
        user_password: ""
      },
      rules: {
        user_name: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        user_password: [
          { required: true, message: "请输入密码", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm() {
      this.$axios.post("/api/session", this.loginForm).then(res => {
        if (res.data.code == 0) {
          localStorage.setItem("user.id", res.data.data.user_id);
          localStorage.setItem("user.name", res.data.data.user_name);
          this.$router.push("/index");
        } else {
          this.loginForm.username = "";
          this.loginForm.password = "";
        }
      });
    }
  }
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
}

.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  color: #fff;
}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 160px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
}
</style>